	<?php
	Class chatModel extends AppModel{
		public function  listUnchecked($page){
	// 		if($update){
	// 			$sql = "UPDATE ".$this->table('chat_record')." SET status = 1 WHERE status=0";
	// 			$this->query($sql);
	// 		}
			$pageSize = $this->get_cfg('capacityPooleach');
			$listPage=array('data'=>'','pageLink'=>'');
// 			$where = " p1.status in(1,2,3,7) and  p1.wxid=p2.wxid  order by  p1.addtime desc ";
			$where = " p1.status in(1,2) and  p1.wxid=p2.wxid  order by p1.status ,  p1.addtime desc ";
			$result=$this->get_page(" p1.* ,  p2.nickname ",$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2  ",$where,$page,$pageSize);
			$digestTime = $this->get_cfg('digestTime');
			foreach($result as $k=>$v){
				switch ($v['status']){//审核状态0:未审核;1:审核中;2:审核中不通过；3:审核中通过；4:审核通过 5:审核不通过 6:经过审核未审核7审核中未经过审核的
					case 0:$result[$k]['statusShow'] = "未审核";break;
					case 1:$result[$k]['statusShow'] = "审核中";break;
					case 2:$result[$k]['statusShow'] = "审核不通过";break;
					case 3:$result[$k]['statusShow'] = "审核通过";break;
					case 4:$result[$k]['statusShow'] = "审核通过";break;
					case 5:$result[$k]['statusShow'] = "审核不通过";break;
					case 6:$result[$k]['statusShow'] = "未经过审核";break;
					case 7:$result[$k]['statusShow'] = "未经过审核";break;
				}
				//如果该时间戳超过24小时，则失效
				if($v['addStatus']==1){//加精评论
					if(time()-$v['addDigestTime']>($digestTime*86400)){
						$result[$k]['addStatus'] =0;
						$result[$k]['addDigestTime'] =0;
					}
				}
			}
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where p1.status in(1,2) ";
			$count=$this->getOne($sql);
			//获取审核中通过的数据
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where p1.status=3 ";
			$passCount=$this->getOne($sql);
			$chatPool = $this->get_cfg("chatPool");
			 if($passCount>$chatPool){
				$passCount = $chatPool;
			 }
			$listPage['count']=$count;
			$listPage['passCount']=$passCount;
			return $listPage;
		}
		public function  listUnchecked_0908($page){
	// 		if($update){
	// 			$sql = "UPDATE ".$this->table('chat_record')." SET status = 1 WHERE status=0";
	// 			$this->query($sql);
	// 		}
			$listPage=array('data'=>'','pageLink'=>'');
			$where = " p1.status in(1,2,3) and  p1.wxid=p2.wxid  order by  p1.addtime desc ";
			$result=$this->get_page(" p1.* ,  p2.nickname ",$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2  ",$where,$page,'50');
			foreach($result as $k=>$v){
				switch ($v['status']){//审核状态0:未审核;1:审核中;2:审核中不通过；3:审核中通过；4:审核通过 5:审核不通过 6:经过审核未审核7审核中未经过审核的
					case 0:$result[$k]['statusShow'] = "未审核";break;
					case 1:$result[$k]['statusShow'] = "审核中";break;
					case 2:$result[$k]['statusShow'] = "审核不通过";break;
					case 3:$result[$k]['statusShow'] = "审核通过";break;
					case 4:$result[$k]['statusShow'] = "审核通过";break;
					case 5:$result[$k]['statusShow'] = "审核不通过";break;
					case 6:$result[$k]['statusShow'] = "未经过审核";break;
					case 7:$result[$k]['statusShow'] = "未经过审核";break;
				}
				
			}
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where p1.status=1 ";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		public function cleanNoCheck(){
			$update = time();
			$result = array('error'=>0,'content'=>'清理成功');
			$sql = "UPDATE ".$this->table('chat_record')." SET status=6 , updatetime=".$update." WHERE status = 7  ";//将审核中未经过审核的改成未经过审核的
			$res = $this->query($sql);
			if(!$res){
				$result['error']=1;
				$result['content']="清理失败";
			}
			return $result;
		}
		public function updateStatus($uids,$status){
			if(!$uids){
				return false;
			}
			$update = time();
			$sql = "update ".$this->table('chat_record')." SET status =".$status." , updatetime=".$update." WHERE id in (".$uids.")";
			return $this->query($sql);
		}
		public function get_timestamp()
		{
			$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='timestamp'";
			$is_appid=$this->getOne($sql);
			return  $is_appid;
		}
		public function get_refreshtime()
		{
			$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='refreshtime'";
			$is_appid=$this->getOne($sql);
			return  $is_appid;
		}
		
		public function cleanChatPool(){
			$sql = "UPDATE  ".$this->table("options")." SET op_value= 0 WHERE op_key='lastCycleNum'";
			$this->query($sql);
		}
		public function updateComment(){
			$update = time();
			
			$isOrderBy = $this->get_cfg('isOrderBy');
			$chatPool = $this->get_cfg("chatPool");
			$sql = "SELECT count(*) FROM ".$this->table('chat_record')." WHERE status=3 ";
			$count=$this->getOne($sql);
			
			if($chatPool<$count){
				if($isOrderBy){//顺序
					$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.status=3 and  p1.wxid=p2.wxid  order by  p1.addtime desc LIMIT $chatPool";
				}else{
					$sql = "SELECT id FROM ".$this->table('chat_record')."  WHERE status=3 order by addtime desc";
					$passAll = $this->getAll($sql);
					$num = intval($count/$chatPool);
					$ids = "";
					for($i=0;$i<=$chatPool-1;$i=$i+$num){
						$ids.=$passAll[$i]['id'].",";
					}
					$ids=substr($ids,0,strlen($ids)-1);
					$sql ="SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.id  in ( ".$ids.") AND p1.status=3 and  p1.wxid=p2.wxid  order by  p1.addtime desc LIMIT $chatPool";
				}
			}else{
			$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.status=3 and  p1.wxid=p2.wxid  order by  p1.addtime desc LIMIT $chatPool";
			}
			$result = $this->getAll($sql);
			$res['result'] = $result;
			$count = count($result);
			$res['count']=$count;
			$this->data2json($res);
// 			$sql =" update ".$this->table("chat_record")." SET status=7 ,updatetime=".$update." WHERE status=1";//将审核中的变为审核过了，但是未操作的
//0925
			$sql =" update ".$this->table("chat_record")." SET status=6 ,updatetime=".$update." WHERE status=1";//将审核中的改为未操作的
			$this->query($sql);
			$sql = "update ".$this->table('chat_record')." SET status=4  , updatetime=".$update." WHERE status = 3";//将审核中通过的变为通过
			$this->query($sql);
			$sql = "update ".$this->table('chat_record')." SET status=5  , updatetime=".$update." WHERE status = 2";//将审核中不通过的变为不通过
			$this->query($sql);
			//获取容量池的大小
			$capacityPool = $this->get_cfg('capacityPool');
			$isOrderBy = $this->get_cfg('isOrderBy');
			$sql = "update ".$this->table('chat_record')." SET status=1  , updatetime=".$update." WHERE status = 0  ";
			$ountSql = "SELECT count(*) FROM ".$this->table("chat_record")." WHERE status=0";
			$count = $this->getOne($ountSql);
			if($count){
				if($isOrderBy){//顺序
					$order = " SELECT id FROM ".$this->table('chat_record')." WHERE status=0  order by addtime desc LIMIT ".$capacityPool;
					$all = $this->getAll($order);
					for($i=0;$i<count($all);$i++){
						$ids.=$all[$i]['id'].",";
					}
					$ids=substr($ids,0,strlen($ids)-1);
					$sql = $sql."AND id in ( ".$ids.")";//将未审核的变成审核中
				}else{//否
					if($count<=$capacityPool){//未审核数量少
						$order = " AND 1=1";
						$sql = $sql.$order;
					}else{//数量大
						$allSql = "SELECT id FROM ".$this->table('chat_record')." WHERE status=0 order by addtime desc ";
						$all = $this->getAll($allSql);
						$num = intval($count/$capacityPool);
						$ids = "";
						for($i=0;$i<=$capacityPool-1;$i=$i+$num){
							$ids.=$all[$i]['id'].",";
						}
						$ids=substr($ids,0,strlen($ids)-1);
						$sql = $sql."AND id in ( ".$ids.")";//将未审核的变成审核中
					}
				}
				$this->query($sql);
				
// 				$sql = "UPDATE ".$this->table('chat_record')." SET status=7 , updatetime=".$update." WHERE status = 0  ";//超出容量池的改成未审核的
//0925
				$sql = "UPDATE ".$this->table('chat_record')." SET status=6 , updatetime=".$update." WHERE status = 0  ";//超出容量池的改成未审核的
				$this->query($sql);
			}
		}
		public function updateComment_0908(){
			$update = time();
			$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.status=3 and  p1.wxid=p2.wxid  order by  p1.addtime desc";
			$result = $this->getAll($sql);
			$sql = "SELECT count(*) FROM ".$this->table('chat_record')." WHERE status=3 ";
			$count=$this->getOne($sql);
			$res['result'] = $result;
			$res['count']=$count;
			$this->data2json($res);
			$sql = "update ".$this->table('chat_record')." SET status=4  , updatetime=".$update." WHERE status = 3";//将审核中通过的变为通过
			$this->query($sql);
			$sql = "update ".$this->table('chat_record')." SET status=5  , updatetime=".$update." WHERE status = 2";//将审核中不通过的变为不通过
			$this->query($sql);
			$sql = "update ".$this->table('chat_record')." SET status=1  , updatetime=".$update." WHERE status = 0";//将未审核的变成审核中
			$this->query($sql);
			
		}
		public function updateCommentPass(){
			$update = time();
			//加上一个旁路功能，即把未审核的改为审核通过
			$sql = "update ".$this->table('chat_record')." SET status =3 , updatetime=".$update." WHERE status = 1";
			$this->query($sql);
			//把未经过审核的也改为审核通过
			//0925
// 			$sql = "update ".$this->table('chat_record')." SET status =3 , updatetime=".$update." WHERE status = 7";
// 			$this->query($sql);
			
			$isOrderBy = $this->get_cfg('isOrderBy');
			$chatPool = $this->get_cfg("chatPool");
			$sql = "SELECT count(*) FROM ".$this->table('chat_record')." WHERE status=3 ";
			$count=$this->getOne($sql);
			
			if($chatPool<$count){
				if($isOrderBy){//顺序
					$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.status=3 and  p1.wxid=p2.wxid  order by  p1.addtime desc LIMIT $chatPool";
				}else{
					$sql = "SELECT id FROM ".$this->table('chat_record')."  WHERE status=3 order by addtime desc";
					$passAll = $this->getAll($sql);
					$num = intval($count/$chatPool);
					$ids = "";
					for($i=0;$i<=$chatPool-1;$i=$i+$num){
						$ids.=$passAll[$i]['id'].",";
					}
					$ids=substr($ids,0,strlen($ids)-1);
					$sql ="SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.id  in ( ".$ids.") AND p1.status=3 and  p1.wxid=p2.wxid  order by  p1.addtime desc LIMIT $chatPool"; 
				}
			}else{
				$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.status=3 and  p1.wxid=p2.wxid  order by  p1.addtime desc LIMIT $chatPool";
			}
			$result = $this->getAll($sql);
			$res['result'] = $result;
			$count = count($result);
			$res['count']=$count;
			$this->data2json($res);
			$sql = "update ".$this->table('chat_record')." SET status=4  , updatetime=".$update." WHERE status = 3";//将审核中通过的变为通过
			$this->query($sql);
			$sql = "update ".$this->table('chat_record')." SET status=5  , updatetime=".$update." WHERE status = 2";//将审核中不通过的变为不通过
			$this->query($sql);
			//获取容量池的大小
			$capacityPool = $this->get_cfg('capacityPool');
			$isOrderBy = $this->get_cfg('isOrderBy');
			$sql = "update ".$this->table('chat_record')." SET status=1  , updatetime=".$update." WHERE status = 0  ";
			$ountSql = "SELECT count(*) FROM ".$this->table("chat_record")." WHERE status=0";
			$count = $this->getOne($ountSql);
			if($count){
			if($isOrderBy){//顺序
					$order = " SELECT id FROM ".$this->table('chat_record')." WHERE status=0  order by addtime desc   LIMIT ".$capacityPool;
					$all = $this->getAll($order);
					for($i=0;$i<count($all);$i++){
						$ids.=$all[$i]['id'].",";
					}
					$ids=substr($ids,0,strlen($ids)-1);
					$sql = $sql."AND id in ( ".$ids.")";//将未审核的变成审核中
				}else{//否
					if($count<=$capacityPool){//未审核数量少
						$order = " AND 1=1";
						$sql = $sql.$order;
					}else{//数量大
						$allSql = "SELECT id FROM ".$this->table('chat_record')." WHERE  status=0 order by addtime desc";
						$all = $this->getAll($allSql);
						$num = intval($count/$capacityPool);
						$ids = "";
						for($i=0;$i<=$capacityPool-1;$i=$i+$num){
							$ids.=$all[$i]['id'].",";
						}
						$ids=substr($ids,0,strlen($ids)-1);
						$sql = $sql."AND id in ( ".$ids.")";//将未审核的变成审核中
					}
				}
				$this->query($sql);
				//0925
// 				$sql = "UPDATE ".$this->table('chat_record')." SET status=7 , updatetime=".$update." WHERE status = 0  ";//超出容量池的改成未审核的
				$sql = "UPDATE ".$this->table('chat_record')." SET status=6 , updatetime=".$update." WHERE status = 0  ";//超出容量池的改成未审核的
				$this->query($sql);
			}
			
		}
		public function updateCommentPass_0908(){
			$update = time();
			//加上一个旁路功能，即把未审核的改为审核通过
			$sql = "update ".$this->table('chat_record')." SET status =3 , updatetime=".$update." WHERE status = 1";
			$this->query($sql);
			$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.status=3 and  p1.wxid=p2.wxid  order by  p1.addtime desc";
			$result = $this->getAll($sql);
			$sql = "SELECT count(*) FROM ".$this->table('chat_record')." WHERE status=3 ";
			$count=$this->getOne($sql);
			$res['result'] = $result;
			$res['count']=$count;
			$this->data2json($res);
			$sql = "update ".$this->table('chat_record')." SET status=4  , updatetime=".$update." WHERE status = 3";//将审核中通过的变为通过
			$this->query($sql);
			$sql = "update ".$this->table('chat_record')." SET status=5  , updatetime=".$update." WHERE status = 2";//将审核中不通过的变为不通过
			$this->query($sql);
			$sql = "update ".$this->table('chat_record')." SET status=1  , updatetime=".$update." WHERE status = 0";//将未审核的变成审核中
			$this->query($sql);
		}
		function data2json($res){
			if(count($res['result'])==0){//为空
				$isRepeat = intval($this->get_cfg("isRepeat"));
				if($isRepeat==1){
					return ;
				}
			}
			
			$refresh = intval($this->get_refreshtime())-1;
			$secNum = intval($res['count']/$refresh);//每秒的个数
	// 		foreach ($res['result']  as $k=>$v){
	// 			$top = 0.8*Math.lcg_value()*100;//高度
	// 			$speed = 8*(Math.lcg_value()+1);//所耗时间
				
	// 		}
			$coefficient = $this->get_cfg('coefficient');
			if(!$coefficient){
				$coefficient = 0.8;
			}
			$coefficient = 2-$coefficient;
			$sec = 1;//最开始的时间
			$sql = "UPDATE  ".$this->table("options")." SET op_value= ".count($res['result'])." WHERE op_key='lastCycleNum'";
			$this->query($sql);
			$_top= 1;
			$nnumber = 0;
			$result=array();
			for($temp = 0;$temp < count($res['result']);$temp++){
				//$top = 0.8*lcg_value()*100;//高度
				$_top=$_top+12;
				if($_top>=85){
					$_top=0;
				}
				$speed = $coefficient*10*(lcg_value()+1);//所耗时间
				$res['result'][$temp]['top'] = $_top;
				$res['result'][$temp]['speed'] = $speed;
				$res['result'][$temp]['delay'] = $sec+lcg_value();
				if($secNum==0){
					$var = intval($res['count']%$refresh);
					$secadd = intval($refresh/$res['count']);
					$sec = $sec+$secadd;
				}else{//超过刷新时间
					if($secNum==1){
// 						$chao = count($res['result']) - $refresh;
						
// 						if($nnumber<=$chao){
// 							if($temp%2==0){//0123456
// 								$sec = $sec+1;//12
// 								//
// 							}else{
// 								if($nnumber!=0){
// 									$nnumber++;
// 								}
// 							}
// 						}else{
// 							$sec = $sec+1;
// 						}
						if($temp<$refresh){//
							$sec = $sec+1;
						}else if($temp==$refresh){
							$sec=1;
						}else{
							$sec = $sec+1;
						}
					}else{
						$in = intval($secNum*2);
						if((($temp+1)%$in)==0){//整除
							$sec = $sec+2;
						}
					}
					
				}
				$result[$temp]['top'] = $res['result'][$temp]['top'];
				$result[$temp]['speed'] = $res['result'][$temp]['speed'];
				$result[$temp]['delay'] = $res['result'][$temp]['delay'];
				$result[$temp]['wxid'] = $res['result'][$temp]['wxid'];
				$result[$temp]['headimgurl'] = $res['result'][$temp]['headimgurl'];
				$result[$temp]['comment'] = $res['result'][$temp]['comment'];
			}
			$fp = fopen(ROOT_PATH."chat.json", "w");
			fwrite($fp, json_encode($result));
			fclose($fp);
			
		}
		
		public function  special_list($page){
			$listPage=array('data'=>'','pageLink'=>'');
			$where = " 1=1  order by  p1.addtime desc ";
			$result=$this->get_page(" p1.*,p2.name as group_name,p3.title as hongbao_name ",$this->table('chat_special')." as p1 left join ".$this->table('box_group')." as p2 on p2.id = p1.group_id left join ".$this->table('lucky_info')." as p3 on p3.id = p1.hongbao_id ",$where,$page,'20');
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('chat_special');
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		
		public function get_chat_special($id){
			$sql = "SELECT * From ".$this->table('chat_special')." WHERE id=".$id;
			$result=$this->getRow($sql);
			if($result)
			{
				return $result;
			}
			else
			{
				return $result=array();
			}
		}
		public function del_chat($id)
		{
			$result=array('error'=>0,'content'=>'删除成功');
			if($id)
			{
		
				$sql="DELETE FROM ".$this->table('chat_special')." WHERE  id='".$id."'";
		
				$res=$this->query($sql);
		
				$sql="DELETE FROM ".$this->table('chat_special_rule')." WHERE  special_id='".$id."'";
		
				$res2=$this->query($sql);
				if(!$res||!$res2)
	// 			if(!$res)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
			}
			return $result;
		}
		
		public function changColor($id,$color){
			$sql = "UPDATE ".$this->table('chat_special')." SET background_color = '".$color."' WHERE id = ".$id;
			$res = $this->query($sql);
			if(!$res){
				return false;
			}else{
				return true;
			}
		}
		
		/***********************************************以下是规则管理的方法****************************************************************/
		public function rule_list($page,$sid){
			$listPage=array('data'=>'','pageLink'=>'');
			$where = " special_id = $sid order by addtime desc ";
			$result=$this->get_page(" * ",$this->table('chat_special_rule'),$where,$page,'20');
			foreach($result as $k=>$v){
				switch ($v['status']){
					case 1:$result[$k]['statusShow'] = "时间规则";break;
					case 2:$result[$k]['statusShow'] = "条数规则";break;
					case 3:$result[$k]['statusShow'] = "每日时间规则";break;
					case 4:$result[$k]['statusShow'] = "单日时间规则";break;
				}
					
			}
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('chat_special_rule')."  where  special_id = $sid ";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		
		public function get_chat_special_rule($id){
			$sql = "SELECT * From ".$this->table('chat_special_rule')." WHERE id=".$id;
			$result=$this->getRow($sql);
			if($result)
			{
				return $result;
			}
			else
			{
				return $result=array();
			}
		}
		public function del_rule($id)
		{
			$result=array('error'=>0,'content'=>'删除成功');
			if($id)
			{
				$sql="DELETE FROM ".$this->table('chat_special_rule')." WHERE  id='".$id."'";
				$res=$this->query($sql);
				if(!$res)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
			}
			return $result;
		}
		
		public function  list_record($page,$cfg=array()){
			$where=" ";
			$logic=" AND ";
			foreach($cfg as $k=>$v)
			{	
				if($k=="startTime"){
					$where.=" p1.addtime>=".$v.$logic;
				}elseif ($k=="endTime"){
					$where.=" p1.addtime<=".$v.$logic;
				}elseif($k=="comment"&&$v!=""){
					$where.=" p1.comment like '%".$v."%' ".$logic;
				}elseif($k=="nickname"&&$v!=""){
					$where.=" p2.nickname like '%".$v."%' ".$logic;
				}else{
					switch ($v){//审核状态0:未审核;1:审核中;2:审核中不通过；3:审核中通过；4:审核通过 5:审核不通过
						case 0:break;
						case 1:$where.=" p1.status = 0 ".$logic;break;
						case 2:$where.=" p1.status = 1 ".$logic;break;
						case 3:$where.=" p1.status in (3,4) ".$logic;break;
						case 4:$where.=" p1.status in (2,5) ".$logic;break;
						case 5:$where.=" p1.status in(6,7) ".$logic;break;
					}
				}
			}
			$listPage=array('data'=>'','pageLink'=>'');
			$where1 = "$where p1.wxid=p2.wxid "." order by  p1.addtime desc ";
			$result=$this->get_page2(" p1.* ,  p2.nickname,p2.openid ",$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2  ",$where1,$page,'50');
			$digestTime = $this->get_cfg('digestTime');
			foreach($result as $k=>$v){
				switch ($v['status']){//审核状态0:未审核;1:审核中;2:审核中不通过；3:审核中通过；4:审核通过 5:审核不通过 6:经过审核未审核7审核中未经过审核的
					case 0:$result[$k]['statusShow'] = "未审核";break;
					case 1:$result[$k]['statusShow'] = "审核中";break;
					case 2:$result[$k]['statusShow'] = "审核不通过";break;
					case 3:$result[$k]['statusShow'] = "审核通过";break;
					case 4:$result[$k]['statusShow'] = "审核通过";break;
					case 5:$result[$k]['statusShow'] = "审核不通过";break;
					case 6:$result[$k]['statusShow'] = "未经过审核";break;
					case 7:$result[$k]['statusShow'] = "未经过审核";break;
				}
				//如果该时间戳超过24小时，则失效
				if($v['addStatus']==1){//加精评论
					if(time()-$v['addDigestTime']>($digestTime*86400)){
						$result[$k]['addStatus'] =0;
						$result[$k]['addDigestTime'] =0;
					}
				}
			}
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			if($cfg['nickname']){
				$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 , ".$this->table('wxusers')."  as p2 where  $where1 ";
				$count=$this->getOne($sql);
				$listPage['count']=$count;
				$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 , ".$this->table('wxusers')."  as p2 where p1.status in (3,4)  AND $where1   ";
				$pass_count=$this->getOne($sql);
				$listPage['pass_count']=$pass_count;
				$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 , ".$this->table('wxusers')."  as p2 where p1.status in (2,5) AND $where1    ";
				$nopass_count=$this->getOne($sql);
				$listPage['nopass_count']=$nopass_count;
				$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 , ".$this->table('wxusers')."  as p2 where p1.status in (0,1) AND $where1    ";
				$passing_count=$this->getOne($sql);
				$listPage['passing_count']=$passing_count;
			}else{
				$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where $where  1=1  ";
				$count=$this->getOne($sql);
				$listPage['count']=$count;
				$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where $where  p1.status in (3,4)  ";
				$pass_count=$this->getOne($sql);
				$listPage['pass_count']=$pass_count;
				$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where $where  p1.status in (2,5)  ";
				$nopass_count=$this->getOne($sql);
				$listPage['nopass_count']=$nopass_count;
				$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where $where  p1.status in (0,1)  ";
				$passing_count=$this->getOne($sql);
				$listPage['passing_count']=$passing_count;
			}
			return $listPage;
		}
		public function clearNopass(){
				$result=array('error'=>0,'content'=>'删除成功');
				$sql="DELETE FROM ".$this->table('chat_record')." WHERE  status=6 ";
				$res=$this->query($sql);
				if(!$res){
					$result['error']=1;
					$result['content']='删除失败';
				}
				return $result;
		}
		public function silenceUser($wxid,$status){
			$time =$_SERVER['REQUEST_TIME'];
			$sql = " SELECT count(*) FROM ".$this->table('chat_silence_user')." WHERE wxid=$wxid AND endTime>=".$time;
			$num = $this->getOne($sql);
			if($num){//有值修改
				$sql = " SELECT id FROM ".$this->table('chat_silence_user')." WHERE wxid=$wxid AND endTime>=".$time." LIMIT 1";
				$id = $this->getOne($sql);
				$data['addTime'] = $time;
				$data['status'] = $status;
				$data['wxid'] = $wxid;
				if($status==1){//小时
					$data['endTime'] = $time+3600;
				}else{
					$data['endTime'] = strtotime('+5 day',$time);
				}
				$res = $this->update($data,array("table"=>"chat_silence_user","id"=>$id));
				
			}else{//新增
				$data['addTime'] = $time;
				$data['status'] = $status;
				$data['wxid'] = $wxid;
				if($status==1){//小时
					$data['endTime'] = $time+3600;
				}else{
					$data['endTime'] = strtotime('+5 day',$time);
				}
				$res = $this->insert($data, "chat_silence_user");
			}
			if($res){
				return true;
			}else{
				return false;
			}
		}
		
		public function del_record($id){
			$result=array('error'=>0,'content'=>'删除成功');
			if($id)
			{
				$sql="DELETE FROM ".$this->table('chat_record')." WHERE  id='".$id."'";
				$res=$this->query($sql);
				if(!$res)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
			}
			return $result;
		}
		
		public function silence_list($page){
			$listPage=array('data'=>'','pageLink'=>'');
			$where = "  p1.wxid=p2.wxid  order by  p1.addtime desc ";
			$result=$this->get_page(" p1.* ,  p2.nickname ",$this->table('chat_silence_user')." as p1 , ".$this->table('wxusers')."  as p2  ",$where,$page,'50');
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('chat_silence_user')."  as p1 ";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		
		public function del_silence($id)
		{
			$result=array('error'=>0,'content'=>'删除成功');
			if($id)
			{
				$sql="DELETE FROM ".$this->table('chat_silence_user')." WHERE  id='".$id."'";
				$res=$this->query($sql);
				if(!$res)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
			}
			return $result;
		}
		
		
		public function keyword_list($page){
			$listPage=array('data'=>'','pageLink'=>'');
			$where = " 1=1  order by  addtime desc ";
			$result=$this->get_page(" * ",$this->table('chat_keyword'),$where,$page,'20');
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('chat_keyword')."   where 1=1 ";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		

		
		public function del_keyword($id){
			$result=array('error'=>0,'content'=>'删除成功');
			if($id)
			{
				$sql="DELETE FROM ".$this->table('chat_keyword')." WHERE  id='".$id."'";
				$res=$this->query($sql);
				if(!$res)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
			}
			return $result;
		}
		public function get_status_pass()
		{
			$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='passstatus'";
			$is_appid=$this->getOne($sql);
			return  $is_appid;
		}
		public function update_pass_status($status)
		{
			$sql="update ".$this->table('options')." SET op_value= ".$status." WHERE op_key='passstatus'";
			$this->query($sql);
			$status = $this->get_status_pass();
			return  $status;
		}
		public function saveConfig($info){
			foreach ($info as $k=>$v){
				$sql = "UPDATE ".$this->table('options')." SET op_value='$v'"." WHERE op_key='$k'";
				$res = $this->query($sql);
			}
			return $res;
		}
		public function get_cfg($key='')
		{
			if($key)
			{
				$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='".$key."'";
				$arr=$this->getOne($sql);
		
			}
			else
			{
				$sql="SELECT * FROM ".$this->table('options')." ";
				$arr=array();
				$res=$this->getAll($sql);
		
				if(count($res)>0)
				{
					foreach($res as $k=>$v)
					{
						$arr[$v['op_key']]=$v['op_value'];
					}
				}
			}
		
			return $arr;
		}
		/*******************************加精******************************************/
		public function digest_list($page){
			$pageSize = $this->get_cfg('capacityPooleach');
			$digestTime = $this->get_cfg('digestTime');
			$listPage=array('data'=>'','pageLink'=>'');
			$where = " p1.addStatus=1 and  p1.wxid=p2.wxid  order by  p1.addDigestTime desc ";
			$result=$this->get_page(" p1.* ,  p2.nickname ",$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2  ",$where,$page,30);
			foreach($result as $k=>$v){
				switch ($v['status']){//审核状态0:未审核;1:审核中;2:审核中不通过；3:审核中通过；4:审核通过 5:审核不通过 6:经过审核未审核7审核中未经过审核的
					case 0:$result[$k]['statusShow'] = "未审核";break;
					case 1:$result[$k]['statusShow'] = "审核中";break;
					case 2:$result[$k]['statusShow'] = "审核不通过";break;
					case 3:$result[$k]['statusShow'] = "审核通过";break;
					case 4:$result[$k]['statusShow'] = "审核通过";break;
					case 5:$result[$k]['statusShow'] = "审核不通过";break;
					case 6:$result[$k]['statusShow'] = "未经过审核";break;
					case 7:$result[$k]['statusShow'] = "未经过审核";break;
				}
				//如果该时间戳超过24小时，则失效
				if(time()-$v['addDigestTime']>($digestTime*86400)){
					$result[$k]['addStatusShow'] ="<span style='color:red;'>已失效</span>";
					$result[$k]['addStatus'] =0;
				}else{
					$result[$k]['addStatusShow'] ="有效";
				}
				$result[$k]['EXP']=$v['addDigestTime']+($digestTime*86400);
			}
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where p1.addStatus=1 ";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
			
		}
		public function addDigest($id){
			$result=array('error'=>0,'content'=>'加精成功');
			$time = time();
			$digestTime = $this->get_cfg('digestTime');
	// 		$digestNum = $this->get_cfg('digestNum');
	// 		$sql="SELECT COUNT(*) FROM ".$this->table('chat_record')." WHERE addStatus=1 AND addDigestTime>=".($time-86400);
	// 		$num = $this->getOne($sql);
	// 		if($num>=$digestNum){
	// 			$result['error']=1;
	// 			$result['content']='加精池已满，请清空部分数据后再来添加';
	// 			return $result;
	// 		}
			$sql ="SELECT addStatus FROM ".$this->table('chat_record')." WHERE id=$id";
			
			$status = $this->getOne($sql);
			if($status==0){
				$sql ="UPDATE ".$this->table('chat_record')." SET addStatus=1,addDigestTime=".$time." WHERE id=$id";
			}else{
				//原先加精的二次加精
				$sql ="SELECT addDigestTime FROM ".$this->table('chat_record')." WHERE id=$id";
				$addDigestTime = $this->getOne($sql);
				if($time-$addDigestTime>($digestTime*86400)){//已失效的二次加精
					$sql ="UPDATE ".$this->table('chat_record')." SET addStatus=1,addDigestTime=".$time." WHERE id=$id";
				}else{
					$sql ="UPDATE ".$this->table('chat_record')." SET addStatus=0,addDigestTime=".$time." WHERE id=$id";
				}
			}
			$res=$this->query($sql);
			if(!$res){
				$result['error']=1;
				$result['content']='加精失败';
			}
			return $result;
		}
		public function failDigest($id){
			$digestTime = $this->get_cfg('digestTime');
			$result=array('error'=>0,'content'=>'失效成功');
			$time = time();
			$sql="UPDATE ".$this->table("chat_record")." SET addDigestTime=".($time-($digestTime*86400)-1)." WHERE id = $id";
			$res=$this->query($sql);
			if(!$res){
				$result['error']=1;
				$result['content']='失效成功';
			}
			return $result;
		}
		public function addDigestPool(){
			$digestNum = $this->get_cfg('digestNum');
			$digestTime = $this->get_cfg('digestTime');
			$time = time();
			$sql = "SELECT p1.id FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.addstatus=1 and addDigestTime>=".($time-($digestTime*86400))." and  p1.wxid=p2.wxid  order by  p1.addDigestTime desc LIMIT $digestNum ";
			$res = $this->getAll($sql);
			$ids = '';
			if(count($res)==0){
				$result = array('error'=>1,'content'=>'无有效加精');
				return $result;
			}
			foreach ($res as $k=>$v){
				$ids=$ids.$v['id'].",";
			}
			$ids=substr($ids,0,strlen($ids)-1);
	// 		$resu=$this->updateStatus($ids,0);
			$resu=$this->updateStatus($ids,1);
			if($resu){
				$result=array('error'=>0,'content'=>'成功添加至弹幕池');
			}else{
				$result = array('error'=>1,'content'=>'添加至弹幕池失败');
			}
			//$result = $this->data2json_digest($res);
			return $result;
		}
		public function data2json_digest($res){
			$result=array('error'=>0,'content'=>'成功添加至加精池');
			$fp = fopen(ROOT_PATH."digest.json", "w");
			fwrite($fp, json_encode($res));
			fclose($fp);
			return $result;
		}
		public function updateDigestPool($uids){
			$resu = $this->updateStatus($uids,1);
	// 		$resu = $this->updateStatus($uids,0);
			if($resu){
				$result=array('error'=>0,'content'=>'成功添加至弹幕池');
			}else{
				$result = array('error'=>1,'content'=>'添加至弹幕池失败');
			}
	// 		$digestNum = $this->get_cfg('digestNum');
	// 		$sql ="SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.addstatus=1 and p1.id in (".$uids.") and  p1.wxid=p2.wxid  order by  p1.addDigestTime desc LIMIT $digestNum ";
	// 		$res = $this->getAll($sql);
			
	// 		$result = $this->data2json_digest($res);
			return $result;
		}
		
		public function temCheck($ids){
			$result = array('error'=>1,'content'=>'临时审核失败');
			$res = $this->updateStatus($ids,4);
			if($res){
				$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.id in (".$ids.")  and  p1.wxid=p2.wxid  order by  p1.updatetime desc";
				$res = $this->getAll($sql);
				$sql = "SELECT count(*) FROM ".$this->table("chat_record")."  WHERE id in (".$ids.")" ;
				$count = $this->getOne($sql);
				$append['count'] = $count;
				$append['result'] = $res;
				$this->data2json_append($append);
				$result['error'] = 0;
				$result['content'] = "临时审核成功";
			}
			return $result;
		}
		function data2json_append($res){
			$refresh = intval($this->get_refreshtime())-1;
			$secNum = intval($res['count']/$refresh);//每秒的个数
			$sec = 1;//最开始的时间
			for($temp = 0;$temp < count($res['result']);$temp++){
				$top = 0.8*lcg_value()*100;//高度
				$speed = 8*(lcg_value()+1);//所耗时间
				$res['result'][$temp]['top'] = $top;
				$res['result'][$temp]['speed'] = $speed;
				$res['result'][$temp]['delay'] = $sec+lcg_value();
				if($secNum==0){
					$var = intval($res['count']%$refresh);
					$secadd = intval($refresh/$res['count']);
					$sec = $sec+$secadd;
				}else{
					if((($temp+1)%$secNum)==0){//整除
						$sec++;
					}
				}
			}
			//修改弹幕池的数量
			$sql = "UPDATE  ".$this->table("options")." SET op_value=op_value+".count($res['result'])." WHERE op_key='lastCycleNum'";
			$this->query($sql);
			$json = file(ROOT_PATH."chat.json");
			$oldjson = $json[0];
			$oldarr = var_dump(json_decode($oldjson, true));
			$oldarr = array_merge($oldarr,$res['result']);
			$fp = fopen(ROOT_PATH."chat.json", "w");
			fwrite($fp, json_encode($oldarr));
			fclose($fp);
		}
		public function cleanDigestPool(){
			$sql = "UPDATE ".$this->table('chat_record')." SET addStatus = 0";
			return $this->query($sql);
		}
		public function list_library($page){
			$listPage=array('data'=>'','pageLink'=>'');
			$where1 = " p1.status = 6 and p1.wxid=p2.wxid  order by  p1.addtime desc ";
			$result=$this->get_page(" p1.* ,  p2.nickname ",$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2  ",$where1,$page,'50');
			$digestTime = $this->get_cfg('digestTime');
			foreach($result as $k=>$v){
				switch ($v['status']){//审核状态0:未审核;1:审核中;2:审核中不通过；3:审核中通过；4:审核通过 5:审核不通过 6:经过审核未审核7审核中未经过审核的
					case 0:$result[$k]['statusShow'] = "未审核";break;
					case 1:$result[$k]['statusShow'] = "审核中";break;
					case 2:$result[$k]['statusShow'] = "审核不通过";break;
					case 3:$result[$k]['statusShow'] = "审核通过";break;
					case 4:$result[$k]['statusShow'] = "审核通过";break;
					case 5:$result[$k]['statusShow'] = "审核不通过";break;
					case 6:$result[$k]['statusShow'] = "未经过审核";break;
					case 7:$result[$k]['statusShow'] = "未经过审核";break;
				}
				//如果该时间戳超过24小时，则失效
				if($v['addStatus']==1){//加精评论
					if(time()-$v['addDigestTime']>($digestTime*86400)){
						$result[$k]['addStatus'] =0;
						$result[$k]['addDigestTime'] =0;
					}
				}
			}
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where  p1.status = 6   ";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		public function get_visitor_record($page,$cfg=array()){
			$listPage=array('data'=>'','pageLink'=>'');
			$where=" ";
			$logic=" AND ";
			foreach($cfg as $k=>$v)
			{
				if($k=="startTime"){
					$where.=" time>=".$v.$logic;
				}elseif ($k=="endTime"){
					$where.=" time<=".$v.$logic;
				}else{
					$where.=" $k = '".$v."' ".$logic;
				}
			}
			$where1 = " $where 1=1  order by  time desc ";
			$result=$this->get_page2(" * ",$this->table('chat_visitor_record'),$where1,$page,'50');
			foreach($result as $k=>$v){
				$result[$k]['timeShow'] = date('Y-m-d H:i:s',$v['time']).' - '.date('Y-m-d H:i:s',$v['time']+120);
				switch ($v['status']){
					case 1:$result[$k]['statusShow'] = "UV";break;
					case 2:$result[$k]['statusShow'] = "PV";break;
				}
			}
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('chat_visitor_record')."  WHERE $where 1=1  ";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		
		public function get_uv_pv_2M($now,$time,$refresh){
			$start = $now - ($now-$time)%$refresh;
			$end = $now+ ($refresh-($now-$time)%$refresh);
			$sql = "SELECT count(*) FROM ".$this->table('chat_visitor')." WHERE logintime>=$start AND logintime<=$end ";
			$pv = $this->getOne($sql);
			$sql = "SELECT  count(*) FROM (select id from ".$this->table('chat_visitor')." WHERE logintime>=$start AND logintime<=$end  GROUP BY openid,IP)b ";
			$uv = $this->getOne($sql);
			$result['pv'] = $pv;
			$result['uv'] = $uv;
			return $result;
		}
		
		public function get_visitor_record2($page,$cfg=array(),$page_size=20){
			$pvcount=0;
			$uvcount=0;
			$result = array();
			$today = strtotime(date('Y-m-d',time()));
			$sql = "SELECT time FROM ".$this->table('chat_visitor_record')." ORDER BY time  LIMIT 1";
			$firstTime = $this->getOne($sql);
			if($firstTime){
				$firstTime = strtotime(date('Y-m-d',$firstTime));
				$day  = ($today - $firstTime)/(24*60*60);
				$page_count = ceil($day/$page_size);//总页数
				$result['pagelink'] = $this->create_link3($page_count,$page);
				if($page_size>$day){
					$number = $day;
				}else{
					$number=$page_size;
				}
				$res = $this->get_pvcount_uvcount($day, $today, $cfg);
				$result['pvcount'] = $res['pvcount'];
				$result['uvcount'] = $res['uvcount'];
				//页面数据
				for($i=1;$i<=$number;$i++){
					$num = $page_size*($page-1);
					$date = strtotime('-'.($num+$i).' day ',$today);
					if($cfg['startTime']==""){
						$startTime = strtotime('-'.($num+$i).' day ',$today);//开始时间
					}else{
						$startTime = strtotime(date('Y-m-d'.$cfg['startTime'],strtotime('-'.($num+$i).' day ',$today)));
					}
					if($cfg['endTime']==""){
						$endTime = strtotime('-'.(($num+$i)-1).' day 1 seconds',$today);//结束时间
					}else{
						$endTime = strtotime(date('Y-m-d'.$cfg['endTime'],strtotime('-'.($num+$i).' day ',$today)));
					}
					$where  = " time >=$startTime and time<=$endTime ";
					//pv
					$sql = "SELECT sum(number) as sum FROM ".$this->table("chat_visitor_record")." WHERE $where and status=2 ";
					$res = $this->getRow($sql);
					//start
					$whereStart = " time+120>$startTime and time<$startTime ";
					$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereStart and status=2 LIMIT 1";
					$startRes = $this->getRow($sql);
					$startpvNum = 0;
					$startpvNum = round($startRes['number']*($startTime-$startRes['time'])/120,0);
					//end
					$whereEnd = " time-120<$endTime and time>$endTime ";
					$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereEnd and status=2 LIMIT 1";
					$endRes = $this->getRow($sql);
					$endpvNum =0;
					$endpvNum = round($endRes['number']*($endTime-$endRes['time'])/120,0);
					$result['data'][$i]['time'] = $date;
					if($res){
						$result['data'][$i]['pvnumber'] = $res['sum']+$startpvNum+$endpvNum;
					}else{
						$result['data'][$i]['pvnumber'] = 0;
					}
					//uv
					$sql = "SELECT sum(number) as sum FROM ".$this->table("chat_visitor_record")." WHERE $where and status=1 ";
					$res = $this->getRow($sql);
					//start
					$whereStart = " time+120>$startTime and time<$startTime ";
					$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereStart and status=1 LIMIT 1";
					$startnvNum=0;
					$startRes = $this->getRow($sql);
					$startnvNum = round($startRes['number']*($startTime-$startRes['time'])/120,0);
					//end
					$whereEnd = " time-120<$endTime and time>$endTime ";
					$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereEnd and status=1 LIMIT 1";
					$endnvNum=0;
					$endRes = $this->getRow($sql);
					$endnvNum = round($endRes['number']*($endTime-$endRes['time'])/120,0);
					if($res){
						$result['data'][$i]['uvnumber'] = $res['sum']+$startnvNum+$endnvNum;
					}else{
						$result['data'][$i]['uvnumber'] = 0;
					}
				}
			}
			return $result;
		}
		public function get_pvcount_uvcount($day,$today,$cfg){
			//总数
			for($i=1;$i<=$day;$i++){
				$date = strtotime('-'.$i.' day ',$today);
				if($cfg['startTime']==""){
					$startTime = strtotime('-'.$i.' day ',$today);//开始时间
				}else{
					$startTime = strtotime(date('Y-m-d'.$cfg['startTime'],strtotime('-'.$i.' day ',$today)));
				}
				if($cfg['endTime']==""){
					$endTime = strtotime('-'.($i-1).' day 1 seconds',$today);//结束时间
				}else{
					$endTime = strtotime(date('Y-m-d'.$cfg['endTime'],strtotime('-'.+$i.' day ',$today)));
				}
				$where  = " time >=$startTime and time<=$endTime ";
				//pv
				$sql = "SELECT sum(number) as sum FROM ".$this->table("chat_visitor_record")." WHERE $where and status=2";
				$res = $this->getRow($sql);
				//start
				$whereStart = " time+120>$startTime and time<$startTime ";
				$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereStart and status=2 LIMIT 1";
				$startRes = $this->getRow($sql);
				$startpvNum = 0;
				$startpvNum = round($startRes['number']*($startTime-$startRes['time'])/120,0);
				//end
				$whereEnd = " time-120<$endTime and time>$endTime ";
				$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereEnd and status=2 LIMIT 1";
				$endRes = $this->getRow($sql);
				$endpvNum =0;
				$endpvNum = round($endRes['number']*($endTime-$endRes['time'])/120,0);
				$result['data'][$i]['time'] = $date;
				if($res){
					$pvcount+= $res['sum']+$startpvNum+$endpvNum;
				}
				//uv
				$sql = "SELECT sum(number) as sum FROM ".$this->table("chat_visitor_record")." WHERE $where and status=1";
				$res = $this->getRow($sql);
				//start
				$whereStart = " time+120>$startTime and time<$startTime ";
				$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereStart and status=1 LIMIT 1";
				$startnvNum=0;
				$startRes = $this->getRow($sql);
				$startnvNum = round($startRes['number']*($startTime-$startRes['time'])/120,0);
				//end
				$whereEnd = " time-120<$endTime and time>$endTime ";
				$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereEnd and status=1 LIMIT 1";
				$endnvNum=0;
				$endRes = $this->getRow($sql);
				$endnvNum = round($endRes['number']*($endTime-$endRes['time'])/120,0);
				if($res){
					$uvcount+=$res['sum']+$startnvNum+$endnvNum;
				}
			}
			$result["uvcount"] = $uvcount;
			$result["pvcount"] = $pvcount;
			return $result;
		}
		/*
		 *总数，当前页数
		* 生成分页的链接_样式3(自定义方法)
		* */
		function create_link3($page_count,$curpage=1)
		{
			 
			 
			$url=(preg_replace('(\&page=[0-9]+)', '', $_SERVER['REQUEST_URI']));
			 
			$first_page="<a onclick='loadPage(\"&page=1\")' style='margin: 0 5px;'>首页</a>";
			$last_page="<a onclick='loadPage(\"&page=".$page_count."\")' style='margin: 0 5px;'>尾页</a>";
			$pre_page = ($curpage == 1)? 1 : $curpage - 1;
			$pre_page ="<a onclick='loadPage(\"&page=".$pre_page."\")' style='margin: 0 5px;'>上一页</a>";
			$next_page= ($curpage == $page_count)? $page_count : $curpage + 1 ;
			$next_page="<a onclick='loadPage(\"&page=".$next_page."\")' style='margin: 0 5px;'>下一页</a>";
			$link='';
			if($page_count<=10)
			{
				for($i=1;$i<$page_count+1;$i++)
				{
				 
				if($curpage==$i)
				{
				$link.="<a class='curr_page' href='#' style='margin: 0 5px;'>$i</a>";
				}
				else
				{
				$link.="<a onclick='loadPage(\"&page=".$i."\")' style='margin: 0 5px;'>$i</a>";
				}
						 
						 
				}
				$this->page_link="".$first_page.$pre_page.$link.$next_page.$last_page."";
			}
			else
			{
					if($curpage>4&&$curpage<=$page_count-4)
					{
					for($i=$curpage-2;$i<$curpage+3;$i++)
						{
						if($curpage==$i)
						{
						$link.="<a class='curr_page' href='#'>$i</a>";
						}
						else
						{
						$link.="<a onclick='loadPage(\"&page=".$i."\")' style='margin: 0 5px;' >$i</a>";
						}
						}
						$this->page_link="".$first_page.$pre_page."<a href='javascript:void(0)'>...</a>".$link."<a href='javascript:void(0)'>...</a>".$next_page.$last_page."";
			}
					elseif($curpage>0&&$curpage<=4)
						{
						for($i=1;$i<6;$i++)
						{
						if($curpage==$i)
							{
							$link.="<a class='curr_page' onclick='loadPage(\"&page=".$i."\")' style='margin: 0 5px;'>$i</a>";
					}
					else
						{
						$link.="<a onclick='loadPage(\"&page=".$i."\")' style='margin: 0 5px;'>$i</a>";
					}
	  
							}
							$this->page_link=$first_page.$pre_page.$link."<a href='javascript:void(0)'>...</a>".$next_page.$last_page;
						}
		elseif($curpage>$page_count-5&&$curpage<=$page_count)
				{
				for($i=$page_count-4;$i<$page_count+1;$i++)
				{
				if($curpage==$i)
					{
						$link.="<a class='curr_page' href='#' style='margin: 0 5px;'>$i</a>";
						}
						else
						{
							$link.="<a onclick='loadPage(\"&page=".$i."\")' style='margin: 0 5px;'>$i</a>";
					}
						 
					}
				$this->page_link=$first_page.$pre_page."<a href='javascript:void(0)'>...</a>".$link.$next_page.$last_page."";
			}
			}
			return $this->page_link;
	   }
		public function balance(){
			$result = array('error'=>0,'content'=>'结算成功');
			$sql ="SELECT logintime FROM ".$this->table('chat_visitor')." ORDER BY logintime LIMIT 1";
			$startTime = $this->getOne($sql);
			$sql ="SELECT logintime FROM ".$this->table('chat_visitor')." ORDER BY logintime desc LIMIT 1";
			$endTime = $this->getOne($sql);
			if($startTime){
				$time  = strtotime(date("Y-m-d",$startTime));
				$apart = ($startTime-$time)%120;//
				$start= $startTime-$apart;
				$time  = strtotime(date("Y-m-d",$endTime));
				$apart = ($endTime-$time)%120;//
				$end= $endTime-$apart+120;
				$time = intval(($end-$start)/120);
				for($i=1;$i<$time;$i++){
					$pv=0;$uv=0;
					$sql = "SELECT count(*) FROM ".$this->table('chat_visitor')." WHERE logintime>=$start AND logintime<".($start+120);
					$pv = $this->getOne($sql);
					$pvinfo['status'] =2;
					$pvinfo['number'] = $pv;
					$pvinfo['time'] = $start;
					if($pvinfo['number']!=0){
						$this->insert($pvinfo, "chat_visitor_record");
					}
					$sql = "SELECT  count(*) FROM (select id from ".$this->table('chat_visitor')." WHERE logintime>=$start AND logintime<".($start+120)." GROUP BY openid,IP)b ";
					$uv = $this->getOne($sql);
					$uvinfo['status'] =1;
					$uvinfo['number'] = $uv;
					$uvinfo['time'] = $start;
					if($uvinfo['number']!=0){
						$this->insert($uvinfo, "chat_visitor_record");
					}
					$start = $start+120;
				}
				$sql ="DELETE FROM ".$this->table('chat_visitor');
				$res=$this->query($sql);
			}
			return $result;
		}
		
		
		public function group_list($page){
			$listPage=array('data'=>'','pageLink'=>'');
			$where = " 1=1  order by  addtime desc ";
			$result=$this->get_page(" * ",$this->table('box_group'),$where,$page,'20');
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('box_group')."   where 1=1 ";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		
		
		public function del_group($id){
			$result=array('error'=>0,'content'=>'删除成功');
			if($id)
			{
				$sql="DELETE FROM ".$this->table('box_group')." WHERE  id='".$id."'";
				$res=$this->query($sql);
				$sql="DELETE FROM ".$this->table('box')." WHERE  group_id='".$id."'";
				$res=$this->query($sql);
				if(!$res)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
				
			}
			return $result;
		}
		
		public function del_box($id){
			$result=array('error'=>0,'content'=>'删除成功');
			if($id)
			{
				$sql="DELETE FROM ".$this->table('box')." WHERE  id='".$id."'";
				$res=$this->query($sql);
				if(!$res)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
				
			}
			return $result;
		}
		
		public function box_list($page,$id){
			$listPage=array('data'=>'','pageLink'=>'');
			$where = " 1=1 AND group_id = '".$id."' order by  type , prize ";
			$result=$this->get_page(" p1.*,p2.title as hongbao_name ",$this->table('box')." as p1 left join ".$this->table("lucky_info")." as p2 ON p2.id = p1.hongbao_id",$where,$page,'20');
			$listPage['pagelink']=$this->getpagelink();
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('box')."   where 1=1 AND group_id = '".$id."'";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		
		
		public function getBox($id)
		{
			$sql="SELECT * FROM ".$this->table('box')." WHERE id = '".$id."'";
				
			$result=$this->getRow($sql);
			if($result)
			{
				return $result;
			}
			else
			{
				return $result=array();
			}
		}
		
		public function getGroup($id)
		{
			$sql="SELECT * FROM ".$this->table('box_group')." WHERE id = '".$id."'";
				
			$result=$this->getRow($sql);
			if($result)
			{
				return $result;
			}
			else
			{
				return $result=array();
			}
		}
		
		
		public function getGroups($id = 0)
		{
			$sql="SELECT * FROM ".$this->table('box_group');
				
			$box_group=$this->getAll($sql);
			
			$sql="SELECT group_id FROM ".$this->table('chat_special') ." where group_id> 0 and id != '".$id."'";
				
			$chat_special=$this->getAll($sql);
			
	// 		var_dump($chat_special);
	// 		var_dump($box_group);
			foreach ($box_group as $k => $v)
			{
				
				foreach ($chat_special as $kk => $vv)
				{
						
					if($vv['group_id'] == $v['id'])	
					{
						
						unset($box_group[$k]);
						//echo "aa";
						
					}
				}
				
			}
			
			if($box_group)
			{
				return $box_group;
			}
			else
			{
				return $result=array();
			}
		}
		
		public function getLucky($id,$table="chat_special"){
			$sql="SELECT * FROM ".$this->table('lucky_info')." WHERE status = 1";
			$box_group=$this->getAll($sql);
			if($table=="chat_special"){
				$sql="SELECT hongbao_id FROM ".$this->table('chat_special') ." where hongbao_id> 0 and id != '".$id."'";
				$chat_special3=$this->getAll($sql);
				$sql="SELECT hongbao_id FROM ".$this->table('box') ." where hongbao_id> 0 ";
				$chat_special2=$this->getAll($sql);
				$chat_special = array_merge($chat_special3,$chat_special2);
			}else{
				$sql="SELECT hongbao_id FROM ".$this->table('chat_special') ." where hongbao_id> 0 ";
				$chat_special3=$this->getAll($sql);
				$sql="SELECT hongbao_id FROM ".$this->table('box') ." where hongbao_id> 0 and id != '".$id."'";
				$chat_special2=$this->getAll($sql);
				$chat_special = array_merge($chat_special3,$chat_special2);
			}
			foreach ($box_group as $k => $v)
			{
			
				foreach ($chat_special as $kk => $vv)
				{
			
					if($vv['hongbao_id'] == $v['id'])
					{
						unset($box_group[$k]);
					}
				}
			
			}
				
			if($box_group)
			{
				return $box_group;
			}
			else
			{
				return $result=array();
			}
		}
		
		public function getBoxs($group_id)
		{
			$sql="SELECT * FROM ".$this->table('box')." WHERE group_id = '".$group_id."'";
			
			$result=$this->getAll($sql);
			if($result)
			{
				return $result;
			}
			else
			{
				return $result=array();
			}
		}
		
		public function box_user_list($page,$box_id=0,$group_id=0){
			$listPage=array('data'=>'','pageLink'=>'');
			
			$where = " 1=1 ";
			
			if($box_id>0)
			{
				$where.=" AND p1.box_id = '".$box_id."'";
			}
			
			if($group_id>0)
			{
				$where.=" AND p1.group_id = '".$group_id."'";
			}
			
			$where .= " order by  p1.addtime desc ";
			$result=$this->get_page(" p1.* , p2.nickname , p2.openid , p3.name as group_name ,p4.name as box_name ",$this->table('box_user_record')." AS p1 LEFT JOIN ".$this->table('wxusers')." AS p2  ON p2.wxid = p1.wxid LEFT JOIN  ".$this->table('box_group')." AS p3  ON p3.id = p1.group_id LEFT JOIN  ".$this->table('box')." AS p4  ON p4.id = p1.box_id",$where,$page,'20');
			$listPage['pagelink']=$this->getpagelink();
			
			foreach ($result as $k => $v)
			{
				$result[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
			}
			
			$listPage['data']=$result;
			
			
			$sql="SELECT count(*) FROM ".$this->table('box_user_record')."   as p1 WHERE  ".$where;
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		public function export_pv_uv($cfg=array()){
			$pvcount=0;
			$uvcount=0;
			$result = array();
			$today = strtotime(date('Y-m-d',time()));
			$sql = "SELECT time FROM ".$this->table('chat_visitor_record')." ORDER BY time  LIMIT 1";
			$firstTime = $this->getOne($sql);
			if($firstTime){
				$firstTime = strtotime(date('Y-m-d',$firstTime));
				$day  = ($today - $firstTime)/(24*60*60);
				//页面数据
				for($i=1;$i<=$day;$i++){
					$date = strtotime('-'.$i.' day ',$today);
					if($cfg['startTime']==""){
						$startTime = strtotime('-'.$i.' day ',$today);//开始时间
					}else{
						$startTime = strtotime(date('Y-m-d'.$cfg['startTime'],strtotime('-'.$i.' day ',$today)));
					}
					if($cfg['endTime']==""){
						$endTime = strtotime('-'.($i-1).' day 1 seconds',$today);//结束时间
					}else{
						$endTime = strtotime(date('Y-m-d'.$cfg['endTime'],strtotime('-'.$i.' day ',$today)));
					}
					$where  = " time >=$startTime and time<=$endTime ";
					//pv
					$sql = "SELECT sum(number) as sum FROM ".$this->table("chat_visitor_record")." WHERE $where and status=2 ";
					$res = $this->getRow($sql);
					//start
					$whereStart = " time+120>$startTime and time<$startTime ";
					$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereStart and status=2 LIMIT 1";
					$startRes = $this->getRow($sql);
					$startpvNum = 0;
					$startpvNum = round($startRes['number']*($startTime-$startRes['time'])/120,0);
					//end
					$whereEnd = " time-120<$endTime and time>$endTime ";
					$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereEnd and status=2 LIMIT 1";
					$endRes = $this->getRow($sql);
					$endpvNum =0;
					$endpvNum = round($endRes['number']*($endTime-$endRes['time'])/120,0);
					$result['data'][$i]['time'] =date('Y-m-d ',$date);
					$result['data'][$i]['interval'] = $cfg['startTime'].'-'.$cfg['endTime'];
					if($res){
						$result['data'][$i]['pvnumber'] = $res['sum']+$startpvNum+$endpvNum;
					}else{
						$result['data'][$i]['pvnumber'] = 0;
					}
					//uv
					$sql = "SELECT sum(number) as sum FROM ".$this->table("chat_visitor_record")." WHERE $where and status=1 ";
					$res = $this->getRow($sql);
					//start
					$whereStart = " time+120>$startTime and time<$startTime ";
					$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereStart and status=1 LIMIT 1";
					$startnvNum=0;
					$startRes = $this->getRow($sql);
					$startnvNum = round($startRes['number']*($startTime-$startRes['time'])/120,0);
					//end
					$whereEnd = " time-120<$endTime and time>$endTime ";
					$sql = "SELECT * FROM ".$this->table("chat_visitor_record")." WHERE $whereEnd and status=1 LIMIT 1";
					$endnvNum=0;
					$endRes = $this->getRow($sql);
					$endnvNum = round($endRes['number']*($endTime-$endRes['time'])/120,0);
					if($res){
						$result['data'][$i]['uvnumber'] = $res['sum']+$startnvNum+$endnvNum;
					}else{
						$result['data'][$i]['uvnumber'] = 0;
					}
				}
			}
			return $result['data'];
		}
		
		
		public function export_chat_record($cfg){
			$where=" ";
			$logic=" AND ";
			foreach($cfg as $k=>$v)
			{
				if($k=="startTime"){
					$where.=" p1.addtime>=".$v.$logic;
				}elseif ($k=="endTime"){
					$where.=" p1.addtime<=".$v.$logic;
				}elseif($k=="comment"&&$v!=""){
					$where.=" p1.comment like '%".$v."%' ".$logic;
				}elseif($k=="nickname"&&$v!=""){
					$where.=" p2.nickname like '%".$v."%' ".$logic;
				}
			}
			$listPage=array('data'=>'','pageLink'=>'');
			$where1 = "$where p1.wxid=p2.wxid AND p1.status in (3,4)  order by  p1.addtime desc ";
			$sql = "SELECT p2.nickname,p2.openid,p1.comment,p1.addtime FROM ".$this->table("chat_record")." as p1 , ".$this->table('wxusers')."  as p2 WHERE  $where1";
			$result = $this->getAll($sql);
// 			$result=$this->get_page2(" p1.* ,  p2.nickname,p2.openid ",$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2  ",$where1,$page,'50');
			foreach($result as $k=>$v){
				$result[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
				$result[$k]['comment'] = post_check($result[$k]['comment']);
			}
			return $result;
		}
		
		public function get_lookpu_day($time){
			$startTime = strtotime(date('Y-m-d 19:00:00',$time));
			$endTime = strtotime(date('Y-m-d 20:20:00',$time));
			$sql ="select  q1.number as uv,q2.number as pv,q1.time  FROM (SELECT * FROM ".$this->table("chat_visitor_record")." WHERE STATUS=1 AND time>=$startTime and time<=$endTime)q1,(SELECT * FROM ".$this->table("chat_visitor_record")." WHERE STATUS=2 AND time>=$startTime and time<=$endTime)q2  WHERE q1.time=q2.time order by time";
			$res = $this->getAll($sql);
			foreach ($res as $k=>$v){
				$res[$k]['endTime'] = $v['time']+120;
			}
			return $res;
		}
		
		
		public function get_hongbao_list($page,$id){
			$listPage=array('data'=>'','pageLink'=>'');
				
// 			$where = " 1=1 ";
			$where .= " 1=1 and lid=$id order by id desc ";
			$result=$this->get_page(" * ",$this->table('hongbao_info'),$where,$page,'20');
			$listPage['pagelink']=$this->getpagelink();
				
			foreach ($result as $k => $v){
				if($v['hb_type']==1){
					$result[$k]['hb_typeShow'] = "裂变红包";
				}else{
					$result[$k]['hb_typeShow'] = "普通红包";
				}
				
				if(!$v['sp_ticket']){
					$result[$k]['hb_typeShow'] = "<span style='color:red;'>无效红包</span>";
				}
			}
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('hongbao_info')." WHERE ".$where;
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		
		public function get_hongbao($id){
			$sql = "SELECT * FROM ".$this->table('hongbao')." WHERE id = $id";
			return $this->getRow($sql);
		}
		
		public function get_luckyDraw_list($page){
			$listPage=array('data'=>'','pageLink'=>'');
			
			// 			$where = " 1=1 ";
			$where .= " 1=1  order by  id desc ";
			$result=$this->get_page(" * ",$this->table('lucky_info'),$where,$page,'20');
			$listPage['pagelink']=$this->getpagelink();
			
			foreach ($result as $k => $v){
				if($v['onoff']==0){
					$result[$k]['onoffShow'] = "关闭";
				}else{
					$result[$k]['onoffShow'] = "开启";
				}
				if($v['status']==1){
					$result[$k]['statusShow'] = "已启用";
				}else{
					$result[$k]['statusShow'] = "准备中";
				}
				$sql = "SELECT count(*) FROM ".$this->table("hongbao_info")." WHERE lid = ".$v['id']." AND sp_ticket!='' ";
				$result[$k]["total"] = $this->getOne($sql);
			}
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('lucky_info')." WHERE ".$where;
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		
		public function get_hongbao_list_ticket($id){
			$sql ="SELECT * FROM ".$this->table("hongbao_info")." WHERE lid = $id AND sp_ticket!='' group by sp_ticket";
			$result = $this->getAll($sql);
			$res = array();
			$i=0;
			foreach ($result as $k=>$v){
				$res[$i] = array("ticket"=>$v['sp_ticket']);
				$i++;
			}
			$sql ="SELECT count(*) FROM ".$this->table("hongbao_info")." WHERE lid = $id AND sp_ticket!='' group by sp_ticket";
			$count = $this->getOne($sql);
			$result['total'] = $count;
			$result['list'] = $res;
			return $result;
		}
		
		public function get_luckyDraw($id){
			$sql = "SELECT * FROM ".$this->table('lucky_info')." WHERE id = $id";
			$result =  $this->getRow($sql);
			$sql = "SELECT count(*) FROM ".$this->table("hongbao_info")." WHERE lid = ".$result['id'];
			$result["total"] = $this->getOne($sql);
			return $result;
		}
		
		
		public function get_hongbao_user_list($page,$id){
			$listPage=array('data'=>'','pageLink'=>'');
			
			$where .= " 1=1 and p1.hongbao_id=$id order by addtime desc ";
			$result=$this->get_page(" p1.*,p2.nickname,p2.openid ",$this->table('get_hongbao_user')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,'20');
			$listPage['pagelink']=$this->getpagelink();
			
			foreach ($result as $k => $v){
				
			}
			$listPage['data']=$result;
			$sql="SELECT count(*) FROM ".$this->table('get_hongbao_user')." p1 WHERE ".$where;
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			return $listPage;
		}
		
}

